﻿using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace DrawingObjects
{
    class DrawingToolsBox : GroupBox
    {
        private Bitmap[] _bitmaps;
        private string[] _names;
        private string _activeButtonName;
        private int _buttonWidth;
        private int _buttonHeight;
        private int _buttonsSeparator;

        public DrawingToolsBox()
        {
            _names = new[] { "Line", "Rectangle", "Ellipse", "Circle" };
            _buttonsSeparator = 10;
            Width = 100;
            Height = 180;
            _buttonWidth = Width - (_buttonsSeparator * 2);
            _buttonHeight = (Height - 10 - (_buttonsSeparator * 5)) / _names.Length;
            _bitmaps = new Bitmap[_names.Length];
            for (var i = 0; i < _names.Length; i++)
                _bitmaps[i] = new Bitmap(_buttonWidth, _buttonHeight);
            DrawButtonsLabels();
            AddButtonsControls();
        }

        private void AddButtonsControls()
        {
            Button button;
            var tempTop = 10;
            for (var i = 0; i < _names.Length; i++)
            {
                tempTop += _buttonsSeparator;
                button = new Button
                             {
                                 Name = _names[i],
                                 BackgroundImage = _bitmaps[i],
                                 Left = _buttonsSeparator,
                                 Top = tempTop,
                                 Width = _buttonWidth,
                                 Height = _buttonHeight,
                                 BackColor = Color.FromKnownColor(KnownColor.Control)
                             };
                button.Click += ButtonClick;
                Controls.Add(button);
                tempTop += _buttonHeight;
            }
        }

        private void DrawButtonsLabels()
        {
            string name;
            var sb = new SolidBrush(Color.Black);
            Graphics graphics;
            for (var i = 0; i < _names.Length; i++ )
            {
                name = _names[i];
                graphics = Graphics.FromImage(_bitmaps[i]);
                switch (name)
                {
                    case "Line":
                        {
                            graphics.DrawLine(new Pen(sb), 10, 10, _buttonWidth - 10, _buttonHeight - 10);
                            break;
                        }
                    case "Rectangle":
                        {
                            graphics.FillRectangle(sb, 5, 5, _buttonWidth - 10, _buttonHeight - 10);
                            break;
                        }
                    case "Ellipse":
                        {
                            var rectangle = new Rectangle(5, 5, _buttonWidth - 10, _buttonHeight - 10);
                            graphics.FillEllipse(sb, rectangle);
                            break;
                        }
                    case "Circle":
                        {
                            var rectangle = new Rectangle((_buttonWidth / 2 - _buttonHeight / 2) + 5, 5, _buttonHeight - 10, _buttonHeight - 10);
                            graphics.FillEllipse(sb, rectangle);
                            break;
                        }
                }
                graphics.Dispose();
            }
            sb.Dispose();
        }

        private void ButtonClick(object sender, System.EventArgs e)
        {
            _activeButtonName = ((Button)sender).Name;
            foreach (var control in Controls.Cast<Button>().Where(control => control != null))
            {
                control.BackColor = Color.FromKnownColor(control.Name.Equals(_activeButtonName) 
                    ? KnownColor.ControlDark : KnownColor.Control);
            }
        }

        public string GetSelectedObject()
        {
            return _activeButtonName;
        }
    }
}
